//
//  KLDataSaveTool.m
//  KL
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//  需求,不同模块的数据不一样,所以需要存不同的文件,然后再根据对应的路径来取

#define KLDataSavePath(index) [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ldjson.data", index]]

#import "KLDataSaveTool.h"
#import "Mahua.h"

@implementation KLDataSaveTool

+ (void)saveData:(NSArray *)data index:(NSInteger)index {
    // 取出一个对象
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:nil];
    NSString *fullPath = KLDataSavePath(index);
    [jsonData writeToFile:fullPath atomically:YES];
}

+ (NSArray *)dataWith:(NSInteger)index {
    NSString *fullPath = KLDataSavePath(index);
    NSData *data = [NSData dataWithContentsOfFile:fullPath];
    if (!data) {
        return nil;
    }
    NSArray *dicArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    KLLog(@"%@", dicArray);
    return dicArray;
}
@end
